package com.zy.swagger.service.impl;

import com.zy.swagger.dao.AppManageDao;
import com.zy.swagger.dao.InterfaceManageDao;
import com.zy.swagger.dao.SysUserDao;
import com.zy.swagger.entity.RequestRecord;
import com.zy.swagger.converter.RequestRecordConverter;
import com.zy.swagger.dao.RequestRecordDao;
import com.zy.swagger.dto.RequestRecordDTO;
import com.zy.swagger.service.RequestRecordService;
import com.zy.swagger.vo.RequestRecordVO;
import com.zy.swagger.dto.CommonIdDTO;
import com.zy.swagger.base.BaseDao;
import com.zy.swagger.base.BaseServiceImpl;
import com.zy.swagger.util.PageHelperTool;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * (表t_request_record)服务接口实现类
 *
 * @author ZY
 * @date 2021-04-24 20:14:00
 */
//@DubboService(interfaceClass = RequestRecordService.class)
@Service("requestRecordService")
public class RequestRecordServiceImpl extends BaseServiceImpl<RequestRecord, Integer, Object> implements RequestRecordService {

    /**
     * 注入持久层对象
     */
    @Resource
    private RequestRecordDao dao;

    @Resource
    private AppManageDao appManageDao;

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private InterfaceManageDao interfaceManageDao;

    /**
     * 定义抽象方法getDao获取当前实体Mapper对象
     *
     * @return 当前实体Mapper对象
     */
    @Override
    protected BaseDao<RequestRecord> getDao() {
        return this.dao;
    }

    /**
     * 新增数据
     *
     * @param dto 参数对象
     * @return 视图对象
     */
    @Override
    public RequestRecordVO insert(RequestRecordDTO dto) {
        RequestRecord entity = RequestRecordConverter.convertFromDTO(dto);
        entity.setCreateTime(new Date());
        RequestRecord res = super.insertSelective(entity);
        if (res != null) {
            return RequestRecordConverter.convertToVO(res);
        }
        return null;
    }

    /**
     * 删除对象
     *
     * @param dto 参数对象
     * @return true:删除成功 false:删除失败
     */
    @Override
    public Integer delete(CommonIdDTO dto) {
        return super.deleteByPrimaryKey(Integer.parseInt(dto.getId()));
    }

    /**
     * 更新对象
     *
     * @param dto 待更新的DTO对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    @Override
    public RequestRecordVO update(RequestRecordDTO dto) {
        RequestRecord entity = RequestRecordConverter.convertFromDTO(dto);
        entity.setUpdateTime(new Date());
        int res = super.updateByPrimaryKeySelective(entity);
        if (res == 1) {
            return RequestRecordConverter.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据ID查询数据
     *
     * @param dto 主键DTO对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    @Override
    public RequestRecordVO getById(CommonIdDTO dto) {
        RequestRecord entity = super.getByPrimaryKey(Integer.parseInt(dto.getId()));
        if (entity != null) {
            return RequestRecordConverter.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据条件查询数据集合
     *
     * @param dto 查询条件对象
     * @return 满足条件的数据集合
     */
    @Override
    public List<RequestRecordVO> getList(RequestRecordDTO dto) {
        Example example = new Example(RequestRecord.class);
        // TODO 根据自身业务情况组装查询条件查询列表(下面是示例)
        //Example.Criteria criteria = example.createCriteria();
        //criteria.andEqualTo("id", dto.getId());
        //criteria.andGreaterThanOrEqualTo("createTime", dto.getCreateTimeBegin());
        //criteria.andLessThanOrEqualTo("createTime", dto.getCreateTimeEnd());
        List<RequestRecord> list = super.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            List<RequestRecordVO> retList = new ArrayList<>();
            list.forEach(x -> retList.add(RequestRecordConverter.convertToVO(x)));
            return retList;
        }
        return new ArrayList<>();
    }

    /**
     * 根据条件查询分页
     *
     * @param dto 查询条件
     * @return 满足条件的分页
     */
    @Override
    public PageInfo<RequestRecordVO> getPageInfo(RequestRecordDTO dto) {
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();

        Example example = new Example(RequestRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (dto.getAppId() != null) {
            criteria.andEqualTo("appId", dto.getAppId());
        }
        if (dto.getUserId() != null) {
            criteria.andEqualTo("userId", dto.getUserId());
        }
        if (dto.getStatus() != null) {
            criteria.andEqualTo("status", dto.getStatus());
        }
        if (!StringUtils.isEmpty(dto.getRequestUrl())) {
            criteria.orLike("requestUrl", "%" + dto.getRequestUrl() + "%");
            criteria.orLike("header", "%" + dto.getRequestUrl() + "%");
            criteria.orLike("params", "%" + dto.getRequestUrl() + "%");
            criteria.orLike("resp", "%" + dto.getRequestUrl() + "%");
        }

        PageInfo<RequestRecord> pageInfo = PageHelper.startPage(pageNum, pageSize).setOrderBy("id desc").doSelectPageInfo(() -> super.selectByExample(example));
        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getList())) {
            return PageHelperTool.initPageInfoObj(pageNum, 0, pageSize, new PageInfo<>(Lists.newArrayList()));
        }
        List<RequestRecordVO> retList = new ArrayList<>();
        pageInfo.getList().forEach(x -> {
            RequestRecordVO vo = RequestRecordConverter.convertToVO(x);
            vo.setAppName(appManageDao.selectByPrimaryKey(x.getAppId()).getAppName());
            vo.setUserName(sysUserDao.selectByPrimaryKey(x.getUserId()).getName());
            vo.setInterfaceName(interfaceManageDao.selectByPrimaryKey(x.getInterfaceId()).getName());
            retList.add(vo);
        });
        return PageHelperTool.initPageInfoObj(pageNum, (int) pageInfo.getTotal(), pageSize, new PageInfo<>(retList));
    }
}